<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #box {
      position: fixed;
      width: 300px;
      height: 300px;
      background-color: red;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }

    #box p {
      padding: 20px;
      background-color: #ccc;
    }
  </style>
</head>

<body>
  <div id="box" hidden>
    <p><button onclick="box.hidden = true">x</button></p>
    <p><input type="text"></p>
    <p><button>提交</button></p>
  </div>
  <p><button id="mybtn" onclick="box.hidden = false">打开</button></p>
  <script>
    document.onclick = function (event) {
      if (!box.contains(event.target) && mybtn !== event.target) {
        box.hidden = true;
      }
    }
  </script>

</body>

</html>